#include "ModelSample.h"
#include "Mesh.h"
#include "DirectXTK\WICTextureLoader.h"
ModelSample::ModelSample(ID3D11Device* device, ID3D11DeviceContext* context)
	: SampleBase(device,context),radians(0.0f)
{
	m_effect = std::make_unique<BasicEffect>(device);
	m_effect->EnableDefaultLighting();

	Mesh mesh("../Resources/Models/Suzanne/Suzanne.gltf");
	m_model = GeometricPrimitive::CreateCustom(context,mesh.GetVertices(),mesh.GetIndices());
	m_model->CreateInputLayout(m_effect.get(),
		m_inputLayout.ReleaseAndGetAddressOf());

	m_states = std::make_unique<CommonStates>(device);
	// create Texture
	ThrowIfFailed(
		CreateWICTextureFromFile(device, L"../Resources/Models/Suzanne/Suzanne_BaseColor.png", nullptr,
			m_texture.ReleaseAndGetAddressOf()));
	m_effect->SetTexture(m_texture.Get());
	// MVP
	m_world = Matrix::Identity;
	m_view = Matrix::CreateLookAt(Vector3(2.f, 1.5f, -2.f),
		Vector3::Zero, Vector3::UnitY);
	m_proj = Matrix::CreatePerspectiveFieldOfView(XM_PI / 3.f,
		16.0f / 9.0f, 0.01f, 100.f);
	m_effect->SetView(m_view);
	m_effect->SetProjection(m_proj);
}

ModelSample::~ModelSample()
{

}

void ModelSample::Update(float dt)
{
	radians += dt*0.2f;
	if (radians > XM_2PI)
	{
		dt = 0.0f;
	}
	m_world = Matrix::CreateRotationY(radians);
}

void ModelSample::Render()
{
	m_effect->SetWorld(m_world);
	m_model->Draw(m_effect.get(), m_inputLayout.Get(), false, false, [=] {
		m_context->RSSetState(m_states->CullClockwise());
		auto sampler = m_states->LinearWrap();
		m_context->PSSetSamplers(1, 1, &sampler);
		});
	/*m_model->Draw(m_world, m_view, m_proj, Colors::White, m_texture.Get(),false ,[=]()->void {
		m_context->RSSetState(m_states->CullClockwise());

		});*/
}

void ModelSample::GUI()
{

}  

void ModelSample::DrawFont()
{

}
